package com.tusuo.stds.ui.question;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.tusuo.stds.Constants;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.Question;
import com.tusuo.stds.ui.complete.CompleteQuestionActivity;
import com.tusuo.stds.ui.complete.CompleteQuestionActivity_;
import com.tusuo.stds.ui.questionbank.QuestionBankTempActivity;
import com.tusuo.stds.ui.questionbank.QuestionBankTempActivity_;
import com.tusuo.stds.ui.similarity.SimilarityTempActivity;
import com.tusuo.stds.ui.similarity.SimilarityTempActivity_;
import com.tusuo.stds.util.webview.WebViewManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by developer on 17-6-12.
 */

public class QuestionAdapterForQuestionBank extends PagerAdapter {

    private LevelDetail mDetail;
    private List<View> mListView;
    private List<BridgeWebView> mWebViewList;
    private String mKey;
    private Activity mActivity;
    private List<WebViewManager> mWebViewManagerList;
    private int mModeCode = 0;


   public QuestionAdapterForQuestionBank(List<View> listView, LevelDetail detail, List<WebViewManager> webViewManagerList, String key, Activity activity) {
        mDetail = detail;
        mListView = listView;
        mWebViewManagerList = webViewManagerList;
        mKey = key;
        mActivity = activity;
    }

    public void setQuestionMode(int modeCode) {
        mModeCode = modeCode;
    }

    public void setReviewMode(int index) {
        Question question = mDetail.QuestionList.get(index);
        question.AnswerContent = "";
        WebViewManager webViewManager = mWebViewManagerList.get(index);
        webViewManager.initQuestion(question);
        webViewManager.setReviewMode();
    }

    public void setAnswerContent(int index,String answerContent) {
        if (!TextUtils.isEmpty(answerContent)) {
            Question question = mDetail.QuestionList.get(index);
            question.AnswerContent = answerContent;
        }
    }

    public void setAnswerContentReviewMode(int index) {
        Question question = mDetail.QuestionList.get(index);
        WebViewManager webViewManager = mWebViewManagerList.get(index);
        webViewManager.initQuestion(question);
        webViewManager.setReviewMode();
    }

    public void setAnswerContentReviewModeRepeat(int index) {
        WebViewManager webViewManager = mWebViewManagerList.get(index);
        webViewManager.setReviewMode();
    }

    public void setAnswerMode(int index,List<String> answerContents) {
        Question question = mDetail.QuestionList.get(index);
        if (question == null) {
            return;
        }
        if (index < answerContents.size()) {
            if (!TextUtils.isEmpty(answerContents.get(index))) {
                question.AnswerContent = answerContents.get(index);
            }else {
                question.AnswerContent = "";
            }
        }else {
            question.AnswerContent = "";
        }

        WebViewManager webViewManager = mWebViewManagerList.get(index);
        webViewManager.initQuestion(question);
        webViewManager.setAnswerMode();
    }

    public void setAnswerModeRepeat(int index) {
        WebViewManager webViewManager = mWebViewManagerList.get(index);
        webViewManager.setAnswerMode();
    }

    public int getWebViewManagerSize() {
        return mWebViewManagerList.size();
    }

    @Override
    public int getItemPosition(Object object) {

        return POSITION_NONE;
    }


    @Override
    public int getCount() {
        return mDetail.QuestionList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = new View(mActivity);

        if (position > mDetail.QuestionList.size() - 1) {
            return view;
        }

        view = mListView.get(position);
        // FIXME: view会是null?
        if (view != null && view.getParent() == null) {
            Question question = mDetail.QuestionList.get(position);
            question.AnswerContent = "";

            WebViewManager webViewManager = mWebViewManagerList.get(position);
            webViewManager.init();
            webViewManager.loadUrl(Constants.QUESTION_PAGE_TEMPLATE);
            if (mActivity instanceof QuestionActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((QuestionActivity_) mActivity).mSubmitAnswerHandler);
            } else if (mActivity instanceof SimilarityTempActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((SimilarityTempActivity_) mActivity).mSubmitAnswerHandler);
            } else if (mActivity instanceof QuestionBankTempActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((QuestionBankTempActivity_) mActivity).mSubmitAnswerHandler);
            } else if (mActivity instanceof CompleteQuestionActivity_) {
                webViewManager.registerHandlerSubmitAnswer(((CompleteQuestionActivity) mActivity).mSubmitAnswerHandler);
            }
            webViewManager.initQuestion(question);

            if (mModeCode == 0) {
                // 答题模式
                webViewManager.setAnswerMode();
            } else {
                // 阅题模式
                webViewManager.setReviewMode();
            }

            container.addView(view);
        }

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }




}
